package org.whuims.pois.action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.dao.PaperDao;
import org.whuims.pois.hibernate.BaseDao;
import org.whuims.pois.model.PaperEntity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by asus on 2014/7/9.
 */
public class PaperAction extends DispatchAction {

    BaseDao baseDao = new BaseDao();

    public ActionForward papers(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        String language = request.getParameter("language");
        if (language == null || language.trim().equals("")) {
            language = "all";
        }
        int count = baseDao.find("from PaperEntity where planguage='"+language+"'").size();
        List<PaperEntity> result = new PaperDao().getPapers();
        request.setAttribute("tag", "文献资料");
        request.setAttribute("language", language);
        request.setAttribute("count", count);
        request.setAttribute("result", result);
        return mapping.findForward("success");
    }

    /**
     * 该方法用于查找最新的新闻，取出最近的300条
     */
    public ActionForward paper(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request, HttpServletResponse response) {
        String language = (String)request.getParameter("language");
        List<PaperEntity> result = null;
        if (language!=null && !language.equals("")){
            if (language.equals("cn")){
                result = baseDao.find("from PaperEntity where planguage='chi'");
            }else if(language.equals("en"))
                result = baseDao.find("from PaperEntity where planguage='en'");
            else result = baseDao.find("from PaperEntity");
        }
        int count = result.size();
        request.setAttribute("tag", "文献资料");
        request.setAttribute("language", language);
        request.setAttribute("count", count);
        request.setAttribute("result", result);
        return mapping.findForward("success");
    }
}
